/**
 * author: zhou
 * date: 2017.10.24
 * description: 人员管理中心
 * functions: {
 *      save: 保存人员信息
 *      findAll: 获取全部人员
 *      findOne: 获取人员信息
 *      update: 更新人员
 * }
 * 
 */

const   mongoPromise = require('../../../core/mongo/mongoPromise'),
        utils        = require('../../../utils/utils'),
        User         = require('../../../core/mongo/models/system/userModel');

const userController = {
    /**
     * 保存学校人员信息
     * @param {*} ctx 
     */
    async save(ctx) {
        const { username, name, sex, schoolId, gradeIds, classIds, subjectIds, roleId } = ctx.request.body.data;
        if (!username) {
            ctx.body = { success: false, message: '抱歉,用户名不能为空', state: 10004 };
            return;
        }
        if (!name) {
            ctx.body = { success: false, message: '抱歉,用户真实姓名不能为空', state: 10004 };
            return;
        }
        if (!sex) {
            ctx.body = { success: false, message: '抱歉,用户性别不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '抱歉,用户所属学校不能为空', state: 10004 };
            return;
        }
        if (!gradeIds) {
            ctx.body = { success: false, message: '抱歉,用户所属年级不能为空', state: 10004 };
            return;
        }
        if (!classIds) {
            ctx.body = { success: false, message: '抱歉,用户负责班级不能为空', state: 10004 };
            return;
        }
        if (!subjectIds) {
            ctx.body = { success: false, message: '抱歉,用户负责科目不能为空', state: 10004 };
            return;
        }
        if (!roleId) {
            ctx.body = { success: false, message: '抱歉,用户角色权限不能为空', state: 10004 };
            return;
        }
        const user = new User({
            username,
            password: utils.encrpt(utils.encrpt(`${username}_123456`, 'school client'), 'Well students!China future!'),
            name,
            sex,
            schoolId,
            gradeIds,
            classIds,
            subjectIds,
            roleId,
            type: 2
        });
        await User.create(user).then((doc) => {
            ctx.body = { success: true, data: [], state: 200 };
        },
        (error) => {
            ctx.body = { success: false, message: '抱歉,保存学校用户失败', state: 10003 };
        });
    },
    /**
     * 查询全部用户
     * @param {*} ctx 
     */
    async findAll(ctx) {
        const { schoolId, date, limit } = ctx.request.body.data;
        if (!schoolId) {
            ctx.body = { success: false, message: '学校id不能为空', state: 10004 };
            return;
        }
        const queryParam = {
            schoolId,
            status: { $lt: 3 },
            isSchoolManager: false
        };
        if (date) {
            queryParam.createDate = { $lt: date }
        }
        let query = User.find(queryParam, { password: 0 }).sort({ createDate: -1 });
        if (limit) {
            query = query.limit(limit);
        }
        query = query.populate('gradeIds', { _id: 0, name: 1 })
                    .populate('classIds', { _id: 0, name: 1 })
                    .populate('subjectIds', { _id: 0, name: 1 })
                    .populate('roleId', { _id: 0, name: 1 });
        const result = await mongoPromise(ctx, query);
        if (result) {
            delete queryParam.createDate;
            const countQuery = User.count(queryParam);
            const countResult = await mongoPromise(ctx, countQuery);
            if (countResult !== undefined) {
                ctx.body = { success: true, data: { data: result, count: countResult }, state: 200 };
            }
        }
    },
    /**
     * 查找用户
     * @param {*} ctx 
     */
    async findOne(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '抱歉,用户id不能为空', state: 10004 };
            return;
        }
        const query = User.findById(id, { password: 0 });
        const result = await mongoPromise(ctx, query);
        if (result !== undefined) {
            ctx.body = { success: true, data: result, state: 200 };
        } else {
            ctx.body = { success: false, message: '抱歉,未查找到该用户', state: 200 };
        }
    },
    /**
     * 更新用户
     * @param {*} ctx 
     */
    async update(ctx) {
        const { _id, username, name, sex, schoolId, gradeIds, classIds, subjectIds, roleId } = ctx.request.body.data;
        if (!_id) {
            ctx.body = { success: false, message: '抱歉,用户id不能为空', state: 10004 };
            return;
        }
        if (!username) {
            ctx.body = { success: false, message: '抱歉,用户名不能为空', state: 10004 };
            return;
        }
        if (!name) {
            ctx.body = { success: false, message: '抱歉,用户真实姓名不能为空', state: 10004 };
            return;
        }
        if (!sex) {
            ctx.body = { success: false, message: '抱歉,用户性别不能为空', state: 10004 };
            return;
        }
        if (!schoolId) {
            ctx.body = { success: false, message: '抱歉,用户所属学校不能为空', state: 10004 };
            return;
        }
        if (!gradeIds) {
            ctx.body = { success: false, message: '抱歉,用户所属年级不能为空', state: 10004 };
            return;
        }
        if (!classIds) {
            ctx.body = { success: false, message: '抱歉,用户负责班级不能为空', state: 10004 };
            return;
        }
        if (!subjectIds) {
            ctx.body = { success: false, message: '抱歉,用户负责科目不能为空', state: 10004 };
            return;
        }
        if (!roleId) {
            ctx.body = { success: false, message: '抱歉,用户角色权限不能为空', state: 10004 };
            return;
        }
        const user = {
            username,
            name,
            sex,
            schoolId,
            gradeIds,
            classIds,
            subjectIds,
            roleId
        };
        const query = User.update({ _id }, { $set: user });
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: [], state: 200 };
        }
    }
};

module.exports = userController;